import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.net.*; import java.io.*; import static java.lang.Thread.sleep; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JFrame; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author D3v4sTo */ public class DoS extends JFrame implements WindowListener, ActionListener { private final DatagramSocket socket; String IP_address; //Indirizzo IP del server int UDP_port = 7; //Porta del applicazione lato server, nel nostro caso la port è 7 int n_pacchetti; //Numero di pacchetti da inoltrare public DoS() throws SocketException { socket = new DatagramSocket(); socket.setSoTimeout(1000); // 1000ms = 1s initComponents(); setTitle("DoS By Domenico Festa"); getContentPane().setBackground(new Color(0,0,0)); } public void close_socket() { socket.close(); } public String sendAndReceive( String request, String host, int port) throws UnknownHostException, IOException, SocketTimeoutException { byte[] buffer; DatagramPacket datagram; String answer; // indirizzo IP del destinatario del datagram InetAddress address = InetAddress.getByName(host); // verifica chiusura socket if ( socket.isClosed()) { throw new IOException(); } // trasformazione in array di byte della stringa buffer = request.getBytes("UTF-8"); // costruzione datagram di richiesta datagram = new DatagramPacket( buffer, buffer.length, address, port); socket.send(datagram); // trasmissione datagram di richiesta // attesa ricezione datagram di richiesta (tempo massimo di attesa: 1s) socket.receive(datagram); // verifica indirizzo/porta provenienza datagram di risposta if ( datagram.getAddress().equals(address) && datagram.getPort() == port) { answer = new String( datagram.getData(), 0, datagram.getLength(), "ISO-8859-1"); } else { throw new SocketTimeoutException(); } return answer; } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // private void initComponents() { jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jPanel1 = new javax.swing.JPanel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); Avvio = new javax.swing.JButton(); IP = new javax.swing.JFormattedTextField(); Porta = new javax.swing.JFormattedTextField(); jLabel2 = new javax.swing.JLabel(); jSlider1 = new javax.swing.JSlider(); jLabel3 = new javax.swing.JLabel(); jLabel7 = new javax.swing.JLabel(); jLabel8 = new javax.swing.JLabel(); Mostra = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jScrollPane1 = new javax.swing.JScrollPane(); JTraffico = new javax.swing.JTextArea(); Esci = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setBackground(new java.awt.Color(0, 0, 0)); getContentPane().setLayout(new java.awt.FlowLayout()); jPanel2.setBackground(new java.awt.Color(0, 0, 0)); jLabel1.setFont(new java.awt.Font("Demonized", 0, 24)); // NOI18N jLabel1.setForeground(new java.awt.Color(204, 204, 0)); jLabel1.setText("DoS"); jPanel1.setBackground(new java.awt.Color(0, 0, 0)); jLabel5.setFont(new java.awt.Font("Demonized", 0, 14)); // NOI18N jLabel5.setForeground(new java.awt.Color(153, 153, 0)); jLabel5.setText("Indirizzo IP:"); jLabel6.setFont(new java.awt.Font("Demonized", 0, 14)); // NOI18N jLabel6.setForeground(new java.awt.Color(0, 102, 0)); jLabel6.setText("Numero di Porta:"); Avvio.setFont(new java.awt.Font("Demonized", 0, 14)); // NOI18N Avvio.setForeground(new java.awt.Color(204, 0, 0)); Avvio.setText("Avvia"); Avvio.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { AvvioActionPerformed(evt); } }); IP.setText("127.0.0.1"); IP.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { IPActionPerformed(evt); } }); Porta.setText("7"); Porta.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { PortaActionPerformed(evt); } }); jLabel2.setFont(new java.awt.Font("Demonized", 0, 18)); // NOI18N jLabel2.setForeground(new java.awt.Color(0, 102, 153)); jLabel2.setText("Numero Pacchetti da inoltrare:"); jSlider1.setMinimum(1); jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() { public void mouseMoved(java.awt.event.MouseEvent evt) { jSlider1MouseMoved(evt); } }); jLabel3.setFont(new java.awt.Font("Demonized", 1, 14)); // NOI18N jLabel3.setForeground(new java.awt.Color(255, 255, 255)); jLabel3.setText("50"); jLabel7.setFont(new java.awt.Font("Demonized", 1, 14)); // NOI18N jLabel7.setForeground(new java.awt.Color(255, 255, 255)); jLabel7.setText("100"); jLabel8.setFont(new java.awt.Font("Demonized", 1, 14)); // NOI18N jLabel8.setForeground(new java.awt.Color(255, 255, 255)); jLabel8.setText("1"); Mostra.setFont(new java.awt.Font("Demonized", 0, 14)); // NOI18N Mostra.setForeground(new java.awt.Color(255, 255, 255)); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING) .addComponent(jSlider1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(0, 12, Short.MAX_VALUE) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup() .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(36, 36, 36))) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(Avvio, javax.swing.GroupLayout.DEFAULT_SIZE, 156, Short.MAX_VALUE) .addComponent(IP) .addComponent(Porta)))) .addGap(28, 28, 28)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel8) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(132, 132, 132) .addComponent(jLabel7) .addContainerGap()))) .addGroup(jPanel1Layout.createSequentialGroup() .addGap(105, 105, 105) .addComponent(Mostra, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(IP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(Porta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(18, 18, 18) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent(jLabel8) .addComponent(jLabel7)) .addGap(18, 18, 18) .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(18, 18, 18) .addComponent(Mostra, javax.swing.GroupLayout.DEFAULT_SIZE, 30, Short.MAX_VALUE) .addGap(18, 18, 18) .addComponent(Avvio, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jLabel4.setFont(new java.awt.Font("Segoe Print", 0, 14)); // NOI18N jLabel4.setForeground(new java.awt.Color(153, 255, 255)); jLabel4.setText("By Domenico Festa"); JTraffico.setEditable(false); JTraffico.setColumns(20); JTraffico.setFont(new java.awt.Font("Monospaced", 0, 14)); // NOI18N JTraffico.setRows(5); jScrollPane1.setViewportView(JTraffico); Esci.setFont(new java.awt.Font("Demonized", 0, 14)); // NOI18N Esci.setForeground(new java.awt.Color(204, 0, 0)); Esci.setText("Termina"); Esci.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { EsciActionPerformed(evt); } }); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(138, 138, 138)) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel2Layout.createSequentialGroup() .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 55, Short.MAX_VALUE) .addComponent(Esci, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane1)) .addContainerGap()) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()))) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 149, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(Esci, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(0, 0, 0)) ); getContentPane().add(jPanel2); pack(); }// private void EsciActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: System.exit(0); } private void AvvioActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: String IP = IP_address; int port = UDP_port; String request = "Traffico inviato", answer; DoSClient client; try { for(int i = 0; i < n_pacchetti; i++){ client = new DoSClient(); answer = client.sendAndReceive(request, IP, port); JTraffico.append(String.valueOf( answer + " \n " )); client.close_socket(); } } catch (SocketException exception) { JTraffico.append(String.valueOf("Errore creazione socket! \n")); } catch (UnknownHostException exception) { JTraffico.append(String.valueOf("Indirizzo IP errato! \n")); } catch (SocketTimeoutException exception) { JTraffico.append(String.valueOf("Nessuna risposta dal server! \n")); } catch (IOException exception) { JTraffico.append(String.valueOf("Errore generico di comunicazione! \n")); } } private void PortaActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: UDP_port = Integer.parseInt(Porta.getText()); //trasforma la stringa in un intero } private void IPActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: IP_address = IP.getText(); } private void jSlider1MouseMoved(java.awt.event.MouseEvent evt) { // TODO add your handling code here: n_pacchetti = jSlider1.getValue(); Mostra.setText(" Pacchetti: " + n_pacchetti); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DoS.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // // /* Create and display the form */ java.awt.EventQueue.invokeLater(() -> { try { new DoS().setVisible(true); } catch (SocketException ex) { Logger.getLogger(DoS.class.getName()).log(Level.SEVERE, null, ex); } }); } // Variables declaration - do not modify private javax.swing.JButton Avvio; private javax.swing.JButton Esci; private javax.swing.JFormattedTextField IP; private javax.swing.JTextArea JTraffico; private javax.swing.JLabel Mostra; private javax.swing.JFormattedTextField Porta; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JLabel jLabel8; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JSlider jSlider1; // End of variables declaration @Override public void windowOpened(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void windowClosing(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void windowClosed(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void windowIconified(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void windowDeiconified(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void windowActivated(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void windowDeactivated(WindowEvent we) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } @Override public void actionPerformed(ActionEvent ae) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }